package com.quanyan.orderpay.service.check.account.impl;

import com.quanyan.orderpay.constants.AccountWaterDataSourceConstants;
import com.quanyan.orderpay.constants.AccountWaterTradeTypeConstants;
import com.quanyan.orderpay.service.check.account.WaterMessageDecoder;
import com.quanyan.pay.api.alipay.vo.AliBillDataItem;
import com.quanyan.pay.entity.AccountPayWater;
import com.quanyan.pay.enums.AliTradeTypeEnum;
import org.springframework.stereotype.Component;


/**
 * Created by pp.zuo on 2017/3/29.
 */
@Component("alipayMessageDecoder")
public class AlipayMessageDecoder implements WaterMessageDecoder {
    /**
     * @param messageObject String[]
     * @return
     */
    @Override
    public AccountPayWater decode(Object messageObject) {
        AccountPayWater accountPayWater = new AccountPayWater();

        AliBillDataItem aliBillDataItem = (AliBillDataItem)messageObject;
        accountPayWater.setOutTradeNo(aliBillDataItem.getTradeNo());
        accountPayWater.setAccountTime(aliBillDataItem.getFinishTime());
        accountPayWater.setAmount(aliBillDataItem.getTotalAmount().abs());
        accountPayWater.setDataSource(AccountWaterDataSourceConstants.ALIPAY);
        if (aliBillDataItem.getTradeType().equals(AliTradeTypeEnum.TRADE)) {
            accountPayWater.setOrderNo(aliBillDataItem.getOutTradeNo());
            accountPayWater.setTradeType(AccountWaterTradeTypeConstants.TRADE);
        } else if (aliBillDataItem.getTradeType().equals(AliTradeTypeEnum.REFUND)) {
            accountPayWater.setOrderNo(aliBillDataItem.getOutRequestNo());
            accountPayWater.setTradeType(AccountWaterTradeTypeConstants.REFUND);
        }
        accountPayWater.setBuyerLoginAccount(aliBillDataItem.getBuyerAccount());

        return accountPayWater;
    }
}
